/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ads.presentation;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * This class implements window shown to administrator.
 * <p>
 * It contains components to draw the window, and the logic differentiating
 * when server is initiated or not.
 *
 * @author MFA
 */
public class AdminMainView extends javax.swing.JFrame {
    private ClientControllerInterface controller;
    
    /**
     * Creates new form AdminMainView
     * @param c 
     */
    public AdminMainView(ClientControllerInterface c) {
        controller = c;
        initComponents();

        //Judge whether server has been initialized by administrator
        if(controller.getSystemStatus().isServerInitialized()) {
            setInitializedAppearance();
        } else {
            setNonInitializedAppearance();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        initTestingData = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        initialize = new javax.swing.JButton();
        logout = new javax.swing.JButton();
        serverStatus = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        initTestingData.setText("Initialize with testing data");
        initTestingData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                initTestingDataActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(initTestingData)
                .addContainerGap(360, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(initTestingData)
                .addContainerGap(313, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("with pre-entered data", jPanel3);

        jButton1.setText("Create Floor Map");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        initialize.setText("Initialize (add some offices first!)");
        initialize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                initializeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(initialize))
                .addContainerGap(326, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(initialize)
                .addContainerGap(284, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("manually", jPanel4);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane2)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane2)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Initialize Server", jPanel2);

        logout.setText("Logout");
        logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutActionPerformed(evt);
            }
        });

        serverStatus.setText("serverstatus");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(serverStatus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(logout)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(logout)
                    .addComponent(serverStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Status", new ads.presentation.SystemStatusView(controller));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * handles with the case when admin press the jButton1
     * 
     * @param evt when admin clicked the jbutton1
     */   
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //change state to create floor map
        controller.wantsToCreateFloorMap(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * This method handles the situation where administrator would like to 
     * initiate system with testing data
     * 
     * @param evt 
     */
    private void initTestingDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_initTestingDataActionPerformed
        controller.initializeWithTestingData(this);
    }//GEN-LAST:event_initTestingDataActionPerformed

    /**
     * This method handles with the situation where the admin clicks the 
     * initialize button and would like to initialize the system
     * 
     * @param evt 
     */
    private void initializeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_initializeActionPerformed
        controller.initializeSystem(this);
    }//GEN-LAST:event_initializeActionPerformed

    /**
     * go back to log-in window when administrator clicked the Log-out button
     * 
     * @param evt event that log-out button is clicked
     */
    private void logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutActionPerformed
        controller.stateNonLoggedIn(this);
    }//GEN-LAST:event_logoutActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton initTestingData;
    private javax.swing.JButton initialize;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JButton logout;
    private javax.swing.JLabel serverStatus;
    // End of variables declaration//GEN-END:variables

    /**
     * This method sets up the appearance of admin window when server is not 
     * initialized.
     * <p>
     * It will warn the administrator that server is not initiated right now,
     * and indicate how to initialize it.
     */
    public void setNonInitializedAppearance() {
        serverStatus.setForeground(Color.RED);
        serverStatus.setText("Server non initialized");
        //do not show status tab when server is not initiated
        jTabbedPane1.setEnabledAt(jTabbedPane1.indexOfTab("Status"), false);
        //warn the admin to initialize the server
        jTabbedPane1.setEnabledAt(jTabbedPane1.indexOfTab("Initialize Server"), true);
        jTabbedPane1.setSelectedIndex(jTabbedPane1.indexOfTab("Initialize Server"));
    }

    /**
     * This method sets up the appearance of admin window when server is initialized.
     * <p>
     * It will not show button of "Initialize Server", while the status tab
     * will be shown
     */
    public void setInitializedAppearance() {
        serverStatus.setForeground(Color.decode("#347235"));
        serverStatus.setText("Server initialized");
        jTabbedPane1.setEnabledAt(jTabbedPane1.indexOfTab("Initialize Server"), false);
        jTabbedPane1.setEnabledAt(jTabbedPane1.indexOfTab("Status"), true);
        jTabbedPane1.setSelectedIndex(jTabbedPane1.indexOfTab("Status"));
    }
}
